import fs from 'fs/promises';
import path from 'path';

// JSON数据
const data = [
  {
    name: '科技交易',
    path: '/transactions', // /transactions，更贴合“科技交易”的概念
    children: [
      {
        name: '成果交易',
        path: '/transactions/results' //  /transactions/results，表示“成果交易”
      },
      {
        name: '需求中心',
        path: '/transactions/demands' //  /transactions/demands，表示“需求中心”
      },
      {
        name: '交易规则',
        path: '/transactions/rules' //  /transactions/rules，表示“交易规则”
      },
    ]
  },
  {
    name: '新闻中心',
    path: '/news', // 新闻中心的主路径设为/news
    children: [
      {
        name: '新闻动态',
        path: '/news/dynamics' // 新闻动态的路径设为/news/dynamics
      },
      {
        name: '声明公告',
        path: '/news/announcements' // 声明公告的路径设为/news/announcements
      },
      {
        name: '重要活动',
        path: '/news/events' // 重要活动的路径设为/news/events
      }
    ]
  },
  {
    name: '会议活动',
    path: '/events', // 会议活动的主路径设为/events
    children: [
      {
        name: '历届活动',
        path: '/events/past' // 历届活动的路径设为/events/past
      },
      {
        name: '热点活动',
        path: '/events/highlights' // 热点活动的路径设为/events/highlights
      }
    ]
  },
  {
    name: '线上展厅',
    path: '/online-exhibition', // 线上展厅的主路径设为/online-exhibition
    children: [
      {
        name: '转化案例',
        path: '/online-exhibition/case-studies' // 转化案例的路径设为/online-exhibition/case-studies
      },
      {
        name: '项目展示',
        path: '/online-exhibition/project-showcases' // 项目展示的路径设为/online-exhibition/project-showcases
      }
    ]
  },
  {
    name: '投资机构',
    path: '/investors',
    children: [
      {
        name: '机构介绍',
        path: '/investors/introductions'
      },
      {
        name: '机构动态',
        path: '/investors/news'
      },
      {
        name: '机构申请',
        path: '/investors/application'
      }
    ]
  },
  {
    name: '合作伙伴',
    path: '/partners',
    children: [
      {
        name: '合作伙伴介绍',
        path: '/partners/introductions'
      },
      {
        name: '合作伙伴动态',
        path: '/partners/news'
      },
      {
        name: '合作伙伴申请',
        path: '/partners/application'
      }
    ]
  },

]

function createDirectoryAndFile(directoryPath, fileName) {
  // 创建目录
  fs.mkdir(directoryPath, { recursive: true }, (err) => {
    if (err) throw err;
    console.log(`Directory created: ${directoryPath}`);

    // 在目录中创建文件
    const filePath = path.join(directoryPath, fileName);
    fs.writeFile(filePath, '', (writeErr) => {
      if (writeErr) throw writeErr;
      console.log(`File created: ${filePath}`);
    });
  });
}

function processCategories(categories, baseDir = '') {
  categories.forEach((category) => {
    const dirPath = path.join(baseDir, category.path.slice(1)); // 去掉开头的'/'
    if (category.children) {
      processCategories(category.children, dirPath);
    } else {
      createDirectoryAndFile(dirPath, 'index.vue');
    }
  });
}

// 执行处理
processCategories(data);
